package com.conn;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Title: <br>
 * Description:  <br>
 * Copyright: WangGuangya<br>
 * Company: WangGuangya<br>
 * @author WangGuangya
 * @version 1.0
 * @date 2018-02-23
 */

public class DbCES {
	
	private static HashMap<String, Connection> connMap = new HashMap<String, Connection>();

    static {
		try {
			getConnection();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public static Connection getConnection() {
		return getConnection(null);
	}
    
	public static Connection getConnection(String name) {
		try {
			if (!connMap.containsKey(name)) {
				connMap.put(name, _getConnection(name));
			}
			return connMap.get(name);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static Connection _getConnection(String name) {
		try {
			HashMap<String,String> map = new HashMap<String, String>();		//存放数据源信息
			DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();  
			DocumentBuilder dBuilder = factory.newDocumentBuilder();
			Document document = dBuilder.parse("env.xml");		//读取要目录下的xml文件，并生成新的dom对象

			//取数据源信息参数值
			Node node = null;
			NodeList nodeList = document.getElementsByTagName("DATABASE");  //数据源信息在DATABASE标签中定义
			for(node = nodeList.item(0); node != null; node = node.getNextSibling()){
				if (name == null || name.trim().length() == 0) {	//name为空时取默认数据源
					if (node.getAttributes() != null 
							&& node.getAttributes().getNamedItem("DEFAULT") != null 
							&& node.getAttributes().getNamedItem("DEFAULT").getNodeValue().equals("true")) {  //取到指定数据源，返回
						break;
					}
				} else {
					if (node.getAttributes() != null 
							&& node.getAttributes().getNamedItem("NAME") != null 
							&& node.getAttributes().getNamedItem("NAME").getNodeValue().equals(name)) {  //取到指定数据源，返回
						break;
					}
				}
			}
			
			if (node == null) {
				throw new Exception("未找到指定数据源");
			}

			nodeList = node.getChildNodes();		//取该结点下的所有子结点，包含数据库详细信息
			for(node = nodeList.item(0); node != null; node = node.getNextSibling()) {  //遍历结点，取出数据库详细信息
				if(node.hasChildNodes()){		//该结点有两种身份，一是上层结点子结点，此时无法取得其值；二是叶子结点，通过获取其第一子结点的值来获取标签内text
					map.put(node.getNodeName(), node.getFirstChild().getNodeValue());   //将该结点的标签与标签内数据作为键值对存入map对象
				}
			}
			
			if(map.get("Driver") == null || map.get("Url") == null 
					|| map.get("User") == null || map.get("Password") == null){
				throw new Exception("数据源信息不全面，请重新配置xml中数据源信息");
			}
			
			//生成DataSource数据源对象
			Class.forName(map.get("Driver"));
			return DriverManager.getConnection(map.get("Url"), map.get("User"), map.get("Password"));
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
